package com.hollycrm.unicom.sheet.business.hibernate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.hollycrm.unicom.sheet.lightweight.IDealSheet;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import org.springframework.orm.hibernate.HibernateTemplate;

/**
 * HibernateBusinessSupportHelper
 *
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public class HibernateBusinessSupportHelper {

    public static List listMainDealSheets(Session session, HibernateTemplate ht,
                                          String sheetFlowNO,
                                          Class clazz) throws
        HibernateException {
        List list = new ArrayList();
        Query query = ht.createQuery(session, "from ds in " + clazz +
                                     " where ds.mainSheetFlowNO=?" +
                                     " and ds.parentSheetFlowNO is null order by ds.sheetFlowNO");
        query.setString(0, sheetFlowNO);
        List ql = query.list();
        for (Iterator iter = ql.iterator(); iter.hasNext(); ) {
            IDealSheet ds = (IDealSheet) iter.next();
            //modified by wjn 2005-02-25
            ds.getSheetAlerts().size();
            list.add(ds);
            list.addAll(listDealSheets(session, ht, ds.getSheetFlowNO(), clazz));
        }
        return list;
    }

    public static List listDealSheets(Session session, HibernateTemplate ht,
                                      String sheetFlowNO,
                                      Class clazz) throws
        HibernateException {
        List list = new ArrayList();
        Query query = ht.createQuery(session, "from ds in " + clazz +
                                     " where ds.parentSheetFlowNO=? order by ds.sheetFlowNO");
        listDealSheets(list, query, sheetFlowNO);
        return list;
    }

    public static List listSiblingMainDealSheets(Session session,
                                                 HibernateTemplate ht,
                                                 String mainSheetFlowNO,
                                                 String sheetFlowNO,
                                                 Class clazz) throws
        HibernateException {
        Query query = ht.createQuery(session, "from ds in " + clazz +
                                     " where ds.mainSheetFlowNO=?" +
                                     " and ds.parentSheetFlowNO is null" +
                                     " and ds.sheetFlowNO <> ?");
        query.setString(0, mainSheetFlowNO);
        query.setString(1, sheetFlowNO);

        return query.list();

    }

    public static List listSiblingDealSheets(Session session,
                                             HibernateTemplate ht,
                                             String parentSheetFlowNO,
                                             String sheetFlowNO,
                                             Class clazz) throws
        HibernateException {
        Query query = ht.createQuery(session, "from ds in " + clazz +
                                     " where ds.parentSheetFlowNO=?" +
                                     " and ds.sheetFlowNO <> ?");
        query.setString(0, parentSheetFlowNO);
        query.setString(1, sheetFlowNO);

        return query.list();
    }

    private static void listDealSheets(List list, Query query,
                                       String parentSheetFlowNO) throws
        HibernateException {
        query.setString(0, parentSheetFlowNO);
        List ql = query.list();
        for (Iterator iter = ql.iterator(); iter.hasNext(); ) {
            IDealSheet ds = (IDealSheet) iter.next();
            //modified by wjn 2005-02-25
            ds.getSheetAlerts().size();
            list.add(ds);
            listDealSheets(list, query, ds.getSheetFlowNO());
        }
    }

    public static List listSiblingConfirmSheets(Session session,
                                                HibernateTemplate ht,
                                                String sendSheetFlowNO,
                                                String sheetFlowNO,
                                                Class clazz) throws
        HibernateException {
        Query query = ht.createQuery(session, "from cs in " + clazz +
                                     " where cs.sendSheetFlowNO=?" +
                                     " and cs.sheetFlowNO <> ?");
        query.setString(0, sendSheetFlowNO);
        query.setString(1, sheetFlowNO);

        return query.list();
    }

    public static List listConfirmSheets(Session session, HibernateTemplate ht,
                                         String sheetFlowNO,
                                         Class clazz) throws HibernateException {
        Query query = ht.createQuery(session, "from cs in " + clazz +
                                     " where cs.sendSheetFlowNO=?");
        query.setString(0, sheetFlowNO);
        return query.list();
    }

    public static List listAnswerRecords(Session session, HibernateTemplate ht,
                                         String mainSheetFlowNO, Class clazz) throws
        HibernateException {
        Query query = ht.createQuery(session, "from o in " + clazz +
                                     " where o.mainSheetFlowNO=?");
        query.setString(0, mainSheetFlowNO);
        return query.list();
    }

    public static List listAcceptRecords(Session session, HibernateTemplate ht,
                                         String mainSheetFlowNO,
                                         Class clazz) throws
        HibernateException {
        Query query = ht.createQuery(session, "from o in " + clazz +
                                     " where o.mainSheetFlowNO=? order by o.beginTime desc");
        query.setString(0, mainSheetFlowNO);
        return query.list();
    }

    public static List querySheetModifyHistory(Session session,
                                               HibernateTemplate ht,
                                               String entityID,
                                               String type, Class clazz) throws
        HibernateException {
        Query query = ht.createQuery(session, "from o in " + clazz +
                                     " where o.entityID=? and o.entityForm=?");
        query.setString(0, entityID);
        query.setCharacter(1, type.charAt(0));
        return query.list();
    }

    public static List listSheetRoutineNote(Session session,
                                            HibernateTemplate ht,
                                            String sheetFlowNO,
                                            Class clazz) throws
        HibernateException {
        Query query = ht.createQuery(session, "from o in " + clazz +
                                     " where o.sheetFlowNO=? order by o.beginTime desc");
        query.setString(0, sheetFlowNO);
        return query.list();
    }

    public static List listSheetAppu(Session session, HibernateTemplate ht,
                                     String sheetFlowNO,
                                     Class clazz) throws HibernateException {
        Query query = ht.createQuery(session, "from o in " + clazz +
                                     " where o.sheetFlowNO=?");
        query.setString(0, sheetFlowNO);
        return query.list();
    }

}
